<?php


namespace CbjCode\General\Library\Traits;

use CbjCode\General\Library\DesignVue\src\components\StringFormat;
use Illuminate\Pagination\Paginator;

/**
 * 模型公共类型
 * Trait CommonModel
 * @property $schemasFields
 * @property $searchFields
 * @package CbjCode\General\Library\Traits
 */
trait CommonModel
{
    /**
     * 表单schemas数据
     * @var array
     */
    protected $schemasFields = [];

    /**
     * 搜索表单信息
     * @var array
     */
    protected $searchFields = [];

    /**
     * 设置Schemas表单信息
     */
    public function setSchemas(){
        return [];
    }

    /**
     * @return array|StringFormat|string
     */
    public function getSchemasAttribute(){
        $this->schemasFields = $this->setSchemas();
        return $this->schemasFields?StringFormat::make($this->schemasFields):[];
    }

    public function setSearch(){
        $searchs = $this->searchFields;
        if ($this->searchFields){

        }
    }

    public function paginate($perPage = null, $columns = ['*'], $page = null, $pageName = 'page'){
        $page = $page ?: Paginator::resolveCurrentPage($pageName);
        $perPage = $perPage ?: $this->getPerPage();
        $results = ($total = $this->toBase()->getCountForPagination())
            ? $this->forPage($page, $perPage)->get($columns)
            : $this->newCollection();
        $pages = ceil($total / $perPage);
        return ['items'=>$results,'total'=>$total,'page'=>$page,'pageSize'=>$perPage,'pages'=>$pages];
    }
}